//获取应用实例
const app = getApp()
import api from '../../utils/api'

Page({
  data:{
    retireList:[],
    sumPrice:0,
    content:''
  },
  onShow: function () {
    let retireList = app.globalData.retireList;
    let sumPrice = 0;
    retireList.filter(item=>{
      sumPrice += item.price * item.number;
    })
    sumPrice = sumPrice.toFixed(2);
    this.setData({
      retireList: retireList,
      sumPrice: sumPrice
    })
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  submitRetire: function () {
    let _this = this;
    let user = wx.getStorageSync("user");
    let content = this.data.content;
    let retireList = [];
    let list = this.data.retireList;
    for (var i = 0; i < list.length; i++) {
      let retire = { "patientId": user.bh, "id": list[i].id, "orderNo": list[i].order_no, "content": content };
      retireList.push(retire);
    }
    console.log("退餐参数", retireList)
    api.POST({
      url: 'retire.action',
      params: retireList,
      success: function (res) {
        if (res.data.success == "true") {
          wx.showToast({
            icon: 'none',
            title: '退餐成功',
          })
          _this.updUser();
          wx.switchTab({
            url: '../order/order'
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }

      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '提交失败',
        })
      },
    })

  },
  updUser() {
    let user = wx.getStorageSync("user");
    let param = { "patientId": user.bh };
    api.POST({
      url: 'login.action',
      params: param,
      success: function (res) {
        let success = res.data.success;
        if (success == "true") {
          let user = res.data.data[0]; //replace(/\s+/g, '')
          user.bh = user.bh.replace(/\s+/g, '');
          wx.setStorage({
            key: 'user',
            data: user
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '请求失败',
        })
      },
    })

  },
})